ed Scripts

Contents|Index|Previous|Next

ed Scripts

diff can produce commands that direct the ed text editor to change the first file into the second file. Long ago, this was the only output mode that was suitable for editing one file into another automatically; today, with patch, it is almost obsolete. Use the '-e' or'--ed' option to select this output format. Like the normal format (see Showing Differences Without Context), this output format does not show any context; unlike the normal format, it does not include the information necessary to apply the diff in reverse (to produce the first file if all you have is the second file and the diff). If the file 'd' contains the output of 'diff -e old new', then the command, '(cat d && echo w) | ed - old', edits 'old' to make it a copy of 'new'.

More generally, if 'd1', 'd2', ...,'dN' contain the outputs of 'diff -e old new1', 'diff -e new1 new2', ...,'diff -e newN-1 newN', respectively, then the command, '(cat d1 d2 ...dN && echo w) | ed - old', edits 'old' to make it a copy of 'newN'.

Detailed Description of ed Format

The ed output format consists of one or more hunks of differences. The changes closest to the ends of the files come first so that commands that change the number of lines do not affect how ed interprets line numbers in succeeding commands. ed format hunks look like the following:

change-command
to-file-line
to-file-line...

Because ed uses a single period on a line to indicate the end of input, GNU diff protects lines of changes that contain a single period on a line by writing two periods instead, then writing a subsequent ed command to change the two periods into one. The ed format cannot represent an incomplete line, so if the second file ends in a changed incomplete line, diff reports an error and then pretends that a newline was appended.

There are three types of change commands. Each consists of a line number or comma-separated range of lines in the first file and a single character indicating the kind of change to make. All line numbers are the original line numbers in the file. The types of change commands are:

'la'
Add text from the second file after line l in the first file. For example, '8a' means to add the following lines after line 8 of file 1.

'rc'
Replace the lines in range r in the first file with the following lines. Like a combined add and delete, but more compact. For example, '5,7c' means change lines 5–7 of file 1 to read as the text file 2.

'rd'
Delete the lines in range r from the first file. For example, '5,7d' means delete lines 5–7 of file 1.

Example ed Script

The following is the output of 'diff -e lao tzu' (see Two Sample Input Files for the complete contents of the two files):

11a
They both may be called deep and profound.
Deeper and more profound,
The door of all subtleties!
.
4c
The named is the mother of all things.
.
1,2d